package com.dd.service;

import com.dd.model.ProvinceAQIStatistics;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class ProvinceStatisticService {
    private static final String STATISTICS_JSON_PATH = "src/main/resources/province_aqi_statistics.json";
    private Gson gson;

    public ProvinceStatisticService() {
        gson = new GsonBuilder().setPrettyPrinting().create();
    }

    public List<ProvinceAQIStatistics> getProvinceAQIStatistics() {
        try (FileReader reader = new FileReader(STATISTICS_JSON_PATH)) {
            Type listType = new TypeToken<ArrayList<ProvinceAQIStatistics>>() {}.getType();
            List<ProvinceAQIStatistics> list = gson.fromJson(reader, listType);
            return list != null ? list : new ArrayList<>();
        } catch (IOException e) {
            System.err.println("Error loading province AQI statistics: " + e.getMessage());
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
}
